package com.yiren.dbaa.module.project_edit.model;

import android.app.Activity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.LightCombinationEntity;
import com.yiren.backstage.currency.entity.SingleLightEntity;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.backstage.currency.enums.CarStateNameEnum;
import com.yiren.backstage.currency.enums.CarStateValueEnum;
import com.yiren.backstage.db.dao.bean.SynExamBean;
import com.yiren.backstage.db.dao.cache.LightCombinationCache;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.module.project_edit.contract.GroupLightContract;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lwb
 * @date : 2022/8/18 14:27
 * @description:  灯光组合M
 */
public class GroupLightModel implements GroupLightContract.Model {

    private Activity activity;
    private Gson gson;
    private UserParamConfigBean userParamConfigBean;

    public GroupLightModel(Activity activity) {
        this.activity = activity;
        gson = new Gson();
        userParamConfigBean = LoadConfigApi.loadUserParamConfig(activity);
    }

    @Override
    public List<LightCombinationEntity> getAllLightCombinations() {
        Map<String,LightCombinationEntity> lightCombinationEntityMap = LightCombinationCache.getAllLightCombination();
        List<LightCombinationEntity>  lightCombinationList = new ArrayList<>();
        Iterator<Map.Entry<String,LightCombinationEntity>> iterator = lightCombinationEntityMap.entrySet().iterator();
        HashMap<String,String> lightstate = new LinkedHashMap<>();
        while (iterator.hasNext()){
            Map.Entry<String,LightCombinationEntity> entry = iterator.next();
            List<SingleLightEntity> singleLightEntities = entry.getValue().getSingleLightEntities();
            for(SingleLightEntity singleLight : singleLightEntities){
                String statevalue = "";
                lightstate = new Gson().fromJson(singleLight.getStateValue(), LinkedHashMap.class);
                for (String key : lightstate.keySet()) {
                    if (lightstate.get(key).contains(CarStateValueEnum.OPEN.toString())||lightstate.get(key).contains(CarStateValueEnum.CLOSE.toString())){
                        statevalue= statevalue+ CarStateNameEnum.valueOf(key).getDescription()+":"+ CarStateValueEnum.valueOf(lightstate.get(key)).getName()+",";
                    }else {
                        statevalue= statevalue+CarStateNameEnum.valueOf(key).getDescription()+":"+lightstate.get(key)+",";
                    }
                }
                singleLight.setStateValueShow(statevalue);
            }
            lightCombinationList.add(entry.getValue());
        }
        return lightCombinationList;
    }

    @Override
    public void insertGroupLight(SiteEntity site,LightCombinationEntity lightCombination) {
        SynExamBean synExamBean;
        if (userParamConfigBean.isPartner()){
            synExamBean =  gson.fromJson(site.getPartneModifyVal(),new TypeToken<SynExamBean>() {}.getType());
        }else {
            synExamBean =  gson.fromJson(site.getCoachModifyVal(),new TypeToken<SynExamBean>() {}.getType());
        }
        if (synExamBean == null)synExamBean = new SynExamBean();
        if (synExamBean.getCombinationLightMap() == null) synExamBean.setCombinationLightMap(new HashMap<>());
        synExamBean.getCombinationLightMap().put(lightCombination.getId(),new SynExamBean.ObjectClass<>(lightCombination,1));
        LightCombinationCache.getAllLightCombination().put(lightCombination.getId(),lightCombination);
        if (userParamConfigBean.isPartner()){
            site.setPartneModifyVal(gson.toJson(synExamBean));
        }else {
            site.setCoachModifyVal(gson.toJson(synExamBean));
        }
        UserDbController.getInstance(false).update(site);
    }

    @Override
    public void deleteGroupLight(SiteEntity site,LightCombinationEntity lightCombination) {
        SynExamBean synExamBean;
        if (userParamConfigBean.isPartner()){
            synExamBean =  gson.fromJson(site.getPartneModifyVal(),new TypeToken<SynExamBean>() {}.getType());
        }else {
            synExamBean =  gson.fromJson(site.getCoachModifyVal(),new TypeToken<SynExamBean>() {}.getType());
        }
        if (synExamBean == null)synExamBean = new SynExamBean();
        if (synExamBean.getCombinationLightMap() == null) synExamBean.setCombinationLightMap(new HashMap<>());
        if (synExamBean.getCombinationLightMap().containsKey(lightCombination.getId())){
            synExamBean.getCombinationLightMap().remove(lightCombination.getId());
        }else {
            synExamBean.getCombinationLightMap().put(lightCombination.getId(),new SynExamBean.ObjectClass<>(null,3));
        }

        LightCombinationCache.getAllLightCombination().remove(lightCombination.getId());
        if (userParamConfigBean.isPartner()){
            site.setPartneModifyVal(gson.toJson(synExamBean));
        }else {
            site.setCoachModifyVal(gson.toJson(synExamBean));
        }
        UserDbController.getInstance(false).update(site);
    }
    @Override
    public void updateGroupLight(SiteEntity site,LightCombinationEntity lightCombination) {
        SynExamBean synExamBean;
        if (userParamConfigBean.isPartner()){
            synExamBean =  gson.fromJson(site.getPartneModifyVal(),new TypeToken<SynExamBean>() {}.getType());
        }else {
            synExamBean =  gson.fromJson(site.getCoachModifyVal(),new TypeToken<SynExamBean>() {}.getType());
        }
        if (synExamBean == null)synExamBean = new SynExamBean();
        if (synExamBean.getCombinationLightMap() == null) synExamBean.setCombinationLightMap(new HashMap<>());
        LightCombinationEntity lightCombination_map = null;
        try {
            lightCombination_map = (LightCombinationEntity) lightCombination.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        lightCombination_map.setSingleLightEntities(null);
        synExamBean.getCombinationLightMap().put(lightCombination.getId(),new SynExamBean.ObjectClass<>(lightCombination_map,2));
        LightCombinationCache.getAllLightCombination().put(lightCombination.getId(),lightCombination);
        if (userParamConfigBean.isPartner()){
            site.setPartneModifyVal(gson.toJson(synExamBean));
        }else {
            site.setCoachModifyVal(gson.toJson(synExamBean));
        }
        UserDbController.getInstance(false).update(site);


    }
}
